package com.leetcode.algorithm.y22.m07.w3;

import com.leetcode.algorithm.common.TreeNode;

/**
 * 110. 平衡二叉树
 * 
 * https://leetcode.cn/problems/balanced-binary-tree/
 * 
 * @author jie.deng
 *
 */
class Question0110Solution02 {

	public boolean isBalanced(TreeNode root) {
		return getHeight(root) >= 0;
	}

	private int getHeight(TreeNode node) {
		if (node == null) {
			return 0;
		}
		int heightL = getHeight(node.left);
		if (heightL == -1) {
			return -1;
		}
		int heightR = getHeight(node.right);
		if (heightR == -1) {
			return -1;
		}
		if (Math.abs(heightL - heightR) > 1) {
			return -1;
		}
		return 1 + Math.max(heightL, heightR);
	}

}